#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys

reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append('/etc/moshimol/')
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
                                             os.pardir)))
import web

apis = ('env', 'check', )

render_apis = ()


class dispatch:

    @staticmethod
    def go(ver, api):
        if 0 == apis.count(api):
            return web.notfound()
        else:
            exec 'import ' + api
            data = eval(api + '.go()')
            if 0 == render_apis.count(api):
                web.header('Content-Type', 'application/json;charset=utf-8')
            web.header('Content-Length', len(str(data)))
            web.header('Access-Control-Allow-Origin', '*')
            print ('112')
            return data

    def GET(self, ver, api):
        print ('GET')
        return dispatch.go(ver, api)

    def POST(self, ver, api):
        print ('POST')
        return dispatch.go(ver, api)


def internalerror():
    return web.internalerror('error happen')


app = web.application(('/(.*)/(.*)', 'dispatch'), globals())
app.internalerror = internalerror
application = app.wsgifunc()
